package com.esri.android.map;

import android.util.Log;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.util.d;
import com.esri.core.map.Graphic;
import com.esri.core.map.GraphicsUtil;
import com.esri.core.map.popup.PopupInfo;
import com.esri.core.renderer.Renderer;
import com.esri.core.symbol.Symbol;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Map;
import org.codehaus.jackson.JsonParser;

/* loaded from: classes.dex */
public class GraphicsLayer extends Layer {
    private static final String a = "internalGraphicUniqueID";
    private Renderer b;
    private int c;
    private int d;
    private MarkerRotationMode e;
    protected RenderingMode renderingMode;

    /* loaded from: classes.dex */
    public enum MarkerRotationMode {
        AROUND_MARKER_ORIGIN,
        AROUND_MARKER_CENTER
    }

    /* loaded from: classes.dex */
    public enum RenderingMode {
        STATIC,
        DYNAMIC
    }

    native int nativeAddGraphic(long j, String str, byte[] bArr, String str2, String str3, int i);

    native int[] nativeAddGraphics(long j, ByteBuffer[] byteBufferArr, byte[][] bArr, ByteBuffer[] byteBufferArr2, ByteBuffer[] byteBufferArr3, int[] iArr);

    native long nativeCreateLayer(int i, int i2);

    native String nativeGetAttributes(long j, int i);

    native int nativeGetDrawOrder(long j, int i);

    native byte[] nativeGetGeometry(long j, int i);

    native int[] nativeGetGraphicOIDs(long j, String str);

    native int[] nativeGetGraphics(long j);

    native int[] nativeGetGraphicsAt(long j, double d, double d2, int i, int i2);

    native String nativeGetInfoTemplate(long j, int i);

    native int nativeGetNumberOfGraphics(long j);

    native int nativeGetObjectID(long j, int i, String str);

    native byte[] nativeGetRenderer(long j);

    native int nativeGetSelectionColor(long j);

    native int[] nativeGetSelectionIDs(long j);

    native String nativeGetSymbol(long j, int i);

    native void nativeInitialize(long j, String str, int i, double d, double d2, double d3, double d4);

    native boolean nativeIsGraphicSelected(long j, int i);

    native void nativeMoveGraphic(long j, int i, double d, double d2);

    native void nativeRemoveAll(long j);

    native boolean nativeRemoveAllSelection(long j);

    native void nativeRemoveGraphicById(long j, int i);

    native void nativeRemoveGraphicsById(long j, int[] iArr);

    native void nativeSetGraphicVisible(long j, int i, boolean z);

    native boolean nativeSetRenderer(long j, String str);

    native void nativeSetSelectionColor(long j, int i);

    native boolean nativeSetSelectionIDs(long j, int[] iArr, boolean z);

    native boolean nativeUnselectGraphic(long j, int i);

    native void nativeUpdateGraphic(long j, int i, String str, byte[] bArr, String str2, String str3, int i2);

    native void nativeUpdateGraphicAttributes(long j, int i, String str);

    native void nativeUpdateGraphicGeometry(long j, int i, byte[] bArr);

    native void nativeUpdateGraphicInfoTemplate(long j, int i, String str);

    native void nativeUpdateGraphicSymbol(long j, int i, String str);

    native void nativeUpdateGraphics(long j, int[] iArr, ByteBuffer[] byteBufferArr, byte[][] bArr, ByteBuffer[] byteBufferArr2, ByteBuffer[] byteBufferArr3, int[] iArr2);

    native void nativeUpdateGraphicsDrawOrder(long j, int[] iArr, int[] iArr2);

    native void nativeUpdateGraphicsVisibility(long j);

    public GraphicsLayer() {
        this(RenderingMode.DYNAMIC);
    }

    public GraphicsLayer(RenderingMode renderingMode) {
        super(false);
        this.renderingMode = RenderingMode.DYNAMIC;
        this.c = 0;
        this.d = 0;
        this.e = MarkerRotationMode.AROUND_MARKER_CENTER;
        this.renderingMode = renderingMode;
        this.e = MarkerRotationMode.AROUND_MARKER_CENTER;
        initLayer();
    }

    public GraphicsLayer(MarkerRotationMode markerRotationMode) {
        super(false);
        this.renderingMode = RenderingMode.DYNAMIC;
        this.c = 0;
        this.d = 0;
        this.e = MarkerRotationMode.AROUND_MARKER_CENTER;
        this.renderingMode = RenderingMode.DYNAMIC;
        this.e = markerRotationMode;
        initLayer();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public GraphicsLayer(boolean z) {
        super(false);
        this.renderingMode = RenderingMode.DYNAMIC;
        this.c = 0;
        this.d = 0;
        this.e = MarkerRotationMode.AROUND_MARKER_CENTER;
        if (z) {
            initLayer();
        }
    }

    protected GraphicsLayer(long j) {
        super(j);
        this.renderingMode = RenderingMode.DYNAMIC;
        this.c = 0;
        this.d = 0;
        this.e = MarkerRotationMode.AROUND_MARKER_CENTER;
    }

    public GraphicsLayer(SpatialReference spatialReference, Envelope envelope) {
        this(spatialReference, envelope, RenderingMode.DYNAMIC);
    }

    public GraphicsLayer(SpatialReference spatialReference, Envelope envelope, RenderingMode renderingMode) {
        super(false);
        this.renderingMode = RenderingMode.DYNAMIC;
        this.c = 0;
        this.d = 0;
        this.e = MarkerRotationMode.AROUND_MARKER_CENTER;
        setDefaultSpatialReference(spatialReference);
        setFullExtent(envelope);
        this.renderingMode = renderingMode;
        this.e = MarkerRotationMode.AROUND_MARKER_CENTER;
        initLayer();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.Layer
    public long create() {
        return nativeCreateLayer(this.renderingMode.ordinal(), this.e.ordinal());
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.Layer
    public void initLayer() {
        if (this.nativeHandle == 0) {
            this.nativeHandle = create();
        }
        if (this.nativeHandle != 0) {
            boolean z = true;
            boolean z2 = getDefaultSpatialReference() == null;
            Envelope fullExtent = getFullExtent();
            if (fullExtent != null && !fullExtent.isEmpty()) {
                z = false;
            }
            double xMin = z ? 0.0d : fullExtent.getXMin();
            double xMax = z ? 0.0d : fullExtent.getXMax();
            nativeInitialize(this.nativeHandle, z2 ? null : getDefaultSpatialReference().getText(), z2 ? -1 : getDefaultSpatialReference().getID(), xMin, z ? 0.0d : fullExtent.getYMin(), xMax, z ? 0.0d : fullExtent.getYMax());
            changeStatus(OnStatusChangedListener.STATUS.INITIALIZED);
            return;
        }
        changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_GRAPHICS_LAYER));
    }

    void a(int i, Point point) {
        if (this.nativeHandle == 0 || point == null || point.isEmpty()) {
            return;
        }
        nativeMoveGraphic(this.nativeHandle, i, point.getX(), point.getY());
    }

    public void removeAll() {
        if (this.nativeHandle != 0) {
            nativeRemoveAll(this.nativeHandle);
        }
    }

    public int addGraphic(Graphic graphic) {
        String json;
        if (this.nativeHandle == 0 || graphic == null) {
            return -1;
        }
        Symbol symbol = graphic.getSymbol();
        Geometry geometry = graphic.getGeometry();
        byte[] bArr = null;
        if (symbol == null) {
            json = null;
        } else {
            try {
                json = symbol.toJson();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        if (geometry != null) {
            bArr = GeometryEngine.geometryToEsriShape(geometry);
        }
        int nativeAddGraphic = nativeAddGraphic(this.nativeHandle, json, bArr, null, d.a(graphic.getAttributes()), graphic.getDrawOrder());
        c(graphic.getDrawOrder());
        return nativeAddGraphic;
    }

    public int[] addGraphics(Graphic[] graphicArr) {
        ByteBuffer byteBuffer;
        ByteBuffer byteBuffer2;
        String a2;
        String json;
        if (this.nativeHandle == 0 || graphicArr == null || graphicArr.length <= 0) {
            return new int[0];
        }
        ByteBuffer[] byteBufferArr = new ByteBuffer[graphicArr.length];
        byte[][] bArr = new byte[graphicArr.length];
        ByteBuffer[] byteBufferArr2 = new ByteBuffer[graphicArr.length];
        ByteBuffer[] byteBufferArr3 = new ByteBuffer[graphicArr.length];
        int[] iArr = new int[graphicArr.length];
        int i = this.c;
        int i2 = this.d;
        try {
            int i3 = 0;
            for (Graphic graphic : graphicArr) {
                if (graphic != null) {
                    Symbol symbol = graphic.getSymbol();
                    if (symbol == null || (json = symbol.toJson()) == null) {
                        byteBuffer = null;
                    } else {
                        byte[] bytes = json.getBytes();
                        byteBuffer = ByteBuffer.allocateDirect(bytes.length + 1);
                        byteBuffer.order(ByteOrder.nativeOrder());
                        byteBuffer.put(bytes);
                        byteBuffer.put((byte) 0);
                    }
                    byteBufferArr[i3] = byteBuffer;
                    Geometry geometry = graphic.getGeometry();
                    bArr[i3] = geometry == null ? null : GeometryEngine.geometryToEsriShape(geometry);
                    Map<String, Object> attributes = graphic.getAttributes();
                    if (attributes == null || attributes.isEmpty() || (a2 = d.a(graphic.getAttributes())) == null) {
                        byteBuffer2 = null;
                    } else {
                        byte[] bytes2 = a2.getBytes();
                        byteBuffer2 = ByteBuffer.allocateDirect(bytes2.length + 1);
                        byteBuffer2.order(ByteOrder.nativeOrder());
                        byteBuffer2.put(bytes2);
                        byteBuffer2.put((byte) 0);
                    }
                    byteBufferArr3[i3] = byteBuffer2;
                    iArr[i3] = graphic.getDrawOrder();
                    if (iArr[i3] < i) {
                        i = iArr[i3];
                    }
                    if (iArr[i3] > i2) {
                        i2 = iArr[i3];
                    }
                    i3++;
                }
            }
            this.d = i2;
            this.c = i;
            return nativeAddGraphics(this.nativeHandle, byteBufferArr, bArr, byteBufferArr2, byteBufferArr3, iArr);
        } catch (Exception e) {
            Log.e(com.esri.core.internal.a.a, "Failed to add graphics.", e);
            throw new RuntimeException(e);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:16:0x0030  */
    /* JADX WARN: Removed duplicated region for block: B:25:0x004f A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public com.esri.core.map.Graphic getGraphic(int r7) {
        /*
            r6 = this;
            long r0 = r6.nativeHandle
            byte[] r0 = r6.nativeGetGeometry(r0, r7)
            r1 = 0
            if (r0 != 0) goto L11
            java.lang.String r7 = "ArcGIS"
            java.lang.String r0 = "The UID provided does not map to an existing Graphic."
            android.util.Log.w(r7, r0)
            return r1
        L11:
            com.esri.core.geometry.Geometry$Type r2 = com.esri.core.geometry.Geometry.Type.UNKNOWN
            com.esri.core.geometry.Geometry r0 = com.esri.core.geometry.GeometryEngine.geometryFromEsriShape(r0, r2)
            long r2 = r6.nativeHandle
            java.lang.String r2 = r6.nativeGetSymbol(r2, r7)
            if (r2 == 0) goto L2d
            org.codehaus.jackson.JsonParser r2 = com.esri.core.internal.util.d.c(r2)     // Catch: java.lang.Exception -> L24 java.io.IOException -> L29
            goto L2e
        L24:
            r2 = move-exception
            r2.printStackTrace()
            goto L2d
        L29:
            r2 = move-exception
            r2.printStackTrace()
        L2d:
            r2 = r1
        L2e:
            if (r2 == 0) goto L34
            com.esri.core.symbol.Symbol r1 = com.esri.core.symbol.SymbolHelper.createSymbol(r2)
        L34:
            long r3 = r6.nativeHandle
            java.lang.String r3 = r6.nativeGetAttributes(r3, r7)
            java.util.Map r3 = com.esri.core.internal.util.d.a(r3)
            java.util.HashMap r3 = (java.util.HashMap) r3
            java.lang.Integer r4 = java.lang.Integer.valueOf(r7)
            java.lang.String r5 = "internalGraphicUniqueID"
            r3.put(r5, r4)
            int r4 = r6.getDrawOrder(r7)
            if (r2 == 0) goto L57
            r2.close()     // Catch: java.io.IOException -> L53
            goto L57
        L53:
            r2 = move-exception
            r2.printStackTrace()
        L57:
            com.esri.core.map.Graphic r7 = com.esri.core.map.FeatureUtil.newGraphic(r7, r0, r1, r3, r4)
            return r7
        */
        throw new UnsupportedOperationException("Method not decompiled: com.esri.android.map.GraphicsLayer.getGraphic(int):com.esri.core.map.Graphic");
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public int getObjectID(int i, String str) {
        if (this.nativeHandle != 0) {
            return nativeGetObjectID(this.nativeHandle, i, str);
        }
        return -1;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public void updateGraphicsVisibility() {
        if (this.nativeHandle != 0) {
            nativeUpdateGraphicsVisibility(this.nativeHandle);
        }
    }

    public int[] getGraphicIDs() {
        if (this.nativeHandle == 0) {
            return null;
        }
        return nativeGetGraphics(this.nativeHandle);
    }

    public int[] getGraphicOIDs(String str) {
        if (this.nativeHandle == 0) {
            return null;
        }
        return nativeGetGraphicOIDs(this.nativeHandle, str);
    }

    public int getNumberOfGraphics() {
        if (this.nativeHandle == 0) {
            return 0;
        }
        return nativeGetNumberOfGraphics(this.nativeHandle);
    }

    public void removeGraphic(int i) {
        if (this.nativeHandle != 0) {
            nativeRemoveGraphicById(this.nativeHandle, i);
        }
    }

    public void removeGraphics(int[] iArr) {
        if (this.nativeHandle == 0 || iArr == null || iArr.length <= 0) {
            return;
        }
        nativeRemoveGraphicsById(this.nativeHandle, iArr);
    }

    public void updateGraphic(int i, Graphic graphic) {
        if (this.nativeHandle != 0) {
            try {
                Symbol symbol = graphic.getSymbol();
                Geometry geometry = graphic.getGeometry();
                byte[] bArr = null;
                String json = symbol == null ? null : symbol.toJson();
                if (geometry != null) {
                    bArr = GeometryEngine.geometryToEsriShape(geometry);
                }
                String a2 = d.a(graphic.getAttributes());
                c(graphic.getDrawOrder());
                nativeUpdateGraphic(this.nativeHandle, i, json, bArr, null, a2, graphic.getDrawOrder());
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }
        }
    }

    public void updateGraphics(int[] iArr, Graphic[] graphicArr) {
        ByteBuffer byteBuffer;
        String a2;
        String json;
        if (this.nativeHandle == 0 || iArr == null || graphicArr == null || iArr.length != graphicArr.length || graphicArr.length <= 0) {
            return;
        }
        ByteBuffer[] byteBufferArr = new ByteBuffer[graphicArr.length];
        byte[][] bArr = new byte[graphicArr.length];
        ByteBuffer[] byteBufferArr2 = new ByteBuffer[graphicArr.length];
        ByteBuffer[] byteBufferArr3 = new ByteBuffer[graphicArr.length];
        int[] iArr2 = new int[graphicArr.length];
        int i = this.c;
        int i2 = this.d;
        try {
            int i3 = 0;
            for (Graphic graphic : graphicArr) {
                if (graphic != null) {
                    Symbol symbol = graphic.getSymbol();
                    ByteBuffer byteBuffer2 = null;
                    if (symbol == null || (json = symbol.toJson()) == null) {
                        byteBuffer = null;
                    } else {
                        byte[] bytes = json.getBytes();
                        byteBuffer = ByteBuffer.allocateDirect(bytes.length + 1);
                        byteBuffer.order(ByteOrder.nativeOrder());
                        byteBuffer.put(bytes);
                        byteBuffer.put((byte) 0);
                    }
                    byteBufferArr[i3] = byteBuffer;
                    Geometry geometry = graphic.getGeometry();
                    bArr[i3] = geometry == null ? null : GeometryEngine.geometryToEsriShape(geometry);
                    Map<String, Object> attributes = graphic.getAttributes();
                    if (attributes != null && !attributes.isEmpty() && (a2 = d.a(graphic.getAttributes())) != null) {
                        byte[] bytes2 = a2.getBytes();
                        ByteBuffer allocateDirect = ByteBuffer.allocateDirect(bytes2.length + 1);
                        allocateDirect.order(ByteOrder.nativeOrder());
                        allocateDirect.put(bytes2);
                        allocateDirect.put((byte) 0);
                        byteBuffer2 = allocateDirect;
                    }
                    byteBufferArr3[i3] = byteBuffer2;
                    iArr2[i3] = graphic.getDrawOrder();
                    if (iArr2[i3] < i) {
                        i = iArr2[i3];
                    }
                    if (iArr2[i3] > i2) {
                        i2 = iArr2[i3];
                    }
                    i3++;
                }
            }
            this.d = i2;
            this.c = i;
            nativeUpdateGraphics(this.nativeHandle, iArr, byteBufferArr, bArr, byteBufferArr2, byteBufferArr3, iArr2);
        } catch (Exception e) {
            Log.e(com.esri.core.internal.a.a, "Failed to update graphics.", e);
            throw new RuntimeException(e);
        }
    }

    public void updateGraphic(int i, Geometry geometry) {
        nativeUpdateGraphicGeometry(this.nativeHandle, i, geometry == null ? null : GeometryEngine.geometryToEsriShape(geometry));
    }

    public void updateGraphic(int i, Symbol symbol) {
        try {
            nativeUpdateGraphicSymbol(this.nativeHandle, i, symbol == null ? null : symbol.toJson());
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }

    public void updateGraphic(int i, Map<String, Object> map) {
        if (this.nativeHandle != 0) {
            nativeUpdateGraphicAttributes(this.nativeHandle, i, d.a(map));
        }
    }

    public void updateGraphic(int i, int i2) {
        if (this.nativeHandle != 0) {
            nativeUpdateGraphicsDrawOrder(this.nativeHandle, new int[]{i}, new int[]{i2});
            c(i2);
        }
    }

    public void updateGraphics(int[] iArr, int i) {
        if (this.nativeHandle == 0 || iArr == null || iArr.length <= 0) {
            return;
        }
        int[] iArr2 = new int[iArr.length];
        for (int i2 = 0; i2 < iArr.length; i2++) {
            iArr2[i2] = i;
        }
        nativeUpdateGraphicsDrawOrder(this.nativeHandle, iArr, iArr2);
        c(i);
    }

    public void updateGraphics(int[] iArr, int[] iArr2) {
        if (this.nativeHandle != 0) {
            nativeUpdateGraphicsDrawOrder(this.nativeHandle, iArr, iArr2);
            for (int i : iArr2) {
                c(i);
            }
        }
    }

    public void setGraphicVisible(int i, boolean z) {
        if (this.nativeHandle != 0) {
            nativeSetGraphicVisible(this.nativeHandle, i, z);
        }
    }

    public Renderer getRenderer() {
        try {
            if (this.nativeHandle != 0 && this.b == null) {
                JsonParser a2 = d.a(nativeGetRenderer(this.nativeHandle));
                if (a2 != null) {
                    return d.f(a2);
                }
                return null;
            }
            return this.b;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void setRenderer(Renderer renderer) {
        try {
            this.b = renderer;
            if (this.nativeHandle != 0) {
                nativeSetRenderer(this.nativeHandle, renderer == null ? null : renderer.toJson());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public int[] getGraphicIDs(float f, float f2, int i) {
        return getGraphicIDs(f, f2, i, -1);
    }

    public int[] getGraphicIDs(float f, float f2, int i, int i2) {
        if (this.nativeHandle == 0) {
            return null;
        }
        return nativeGetGraphicsAt(this.nativeHandle, f, f2, i, i2);
    }

    public int getMinDrawOrder() {
        return this.c;
    }

    public int getMaxDrawOrder() {
        return this.d;
    }

    public int getDrawOrder(int i) {
        if (this.nativeHandle != 0) {
            return nativeGetDrawOrder(this.nativeHandle, i);
        }
        return this.c;
    }

    public void bringToFront(int i) {
        int i2 = this.d;
        if (i2 != Integer.MAX_VALUE) {
            i2++;
        }
        updateGraphic(i, i2);
    }

    public void sendToBack(int i) {
        int i2 = this.c;
        if (i2 != Integer.MIN_VALUE) {
            i2--;
        }
        updateGraphic(i, i2);
    }

    public void clearSelection() {
        if (this.nativeHandle != 0) {
            nativeRemoveAllSelection(this.nativeHandle);
        }
    }

    public int[] getSelectionIDs() {
        if (this.nativeHandle != 0) {
            int[] nativeGetSelectionIDs = nativeGetSelectionIDs(this.nativeHandle);
            if (nativeGetSelectionIDs != null) {
                Arrays.sort(nativeGetSelectionIDs);
                return nativeGetSelectionIDs;
            }
            return nativeGetSelectionIDs;
        }
        return null;
    }

    void a(int[] iArr, boolean z) {
        if (this.nativeHandle == 0 || iArr == null) {
            return;
        }
        nativeSetSelectionIDs(this.nativeHandle, iArr, z);
    }

    public void setSelectedGraphics(int[] iArr, boolean z) {
        if (z) {
            a(iArr, z);
        } else if (this.nativeHandle != 0 && iArr != null) {
            for (int i : iArr) {
                nativeUnselectGraphic(this.nativeHandle, i);
            }
        }
    }

    void a(int i) {
        if (this.nativeHandle != 0) {
            nativeUnselectGraphic(this.nativeHandle, i);
        }
    }

    public int getSelectionColor() {
        if (this.nativeHandle != 0) {
            return GraphicsUtil.abgrToColor(nativeGetSelectionColor(this.nativeHandle));
        }
        return 0;
    }

    public void setSelectionColor(int i) {
        if (this.nativeHandle != 0) {
            nativeSetSelectionColor(this.nativeHandle, GraphicsUtil.colorToAbgr(i));
        }
    }

    public void setSelectionColorWidth(int i) {
        super.b(i);
    }

    public boolean isGraphicSelected(int i) {
        if (this.nativeHandle != 0) {
            return nativeIsGraphicSelected(this.nativeHandle, i);
        }
        return false;
    }

    private void c(int i) {
        if (i < this.c) {
            this.c = i;
        }
        if (i > this.d) {
            this.d = i;
        }
    }

    public PopupInfo createPopupInfo(Graphic graphic) {
        PopupInfo popupInfo = new PopupInfo(graphic);
        popupInfo.setTitle(getTitle());
        popupInfo.setMaxScale(getMaxScale());
        popupInfo.setMinScale(getMinScale());
        return popupInfo;
    }
}
